package medicalDocumentation.medicalDoc;

import entity.MedicalProcedure;
import entity.PlanProcedure;
import entity.Treatment;
import java.util.ArrayList;

import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateful
public class TreatmentProcedureSessionBean implements TreatmentProcedureSessionBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private Treatment treatment;
    private List<PlanProcedure> planProcedureList = new ArrayList<>();
    ArrayList<PlanProcedure> res;
    private PlanProcedure planProcedure;

    public TreatmentProcedureSessionBean() {
    }

    @Override
    public List<MedicalProcedure> getProcedure() {
        System.out.println(" Session Bean called");
        String strQuery = "SELECT m from MedicalProcedure m";
        Query query = em.createQuery(strQuery);
        System.out.println(query.getResultList().size());
        return query.getResultList();

    }

    @Override
    public void deleteProcedure(Long treatmentID, Long procedureID) {
        PlanProcedure procedure = em.find(PlanProcedure.class, procedureID);
        Treatment treat = em.find(Treatment.class, treatmentID);
        treat.getProcedureList().remove(procedure);
        em.remove(procedure);
        em.flush();

    }

    @Override
    public ArrayList<PlanProcedure> displayTreatmentProcedure(Long treatmentID) {

        Treatment treat = em.find(Treatment.class, treatmentID);
        res = new ArrayList();

        for (int i = 0; i < treat.getProcedureList().size(); i++) {
            res.add(treat.getProcedureList().get(i));

        }
        return res;
    }

    @Override
    public Long addPlanProcedure(Long treatmentID, String code, String name, String detail, String status, String remark, String price) {

        planProcedure = new PlanProcedure(code, name, detail, status, remark, price);
        MedicalProcedure mp = em.find(MedicalProcedure.class, code);
        planProcedure.setOrderType(mp.getOrderType());
        treatment = em.find(Treatment.class, treatmentID);

        treatment.getProcedureList().add(planProcedure);

        em.persist(planProcedure);

        em.persist(treatment);

        em.flush();
        return planProcedure.getId();
    }

    @Override
    public Treatment getTreatment() {
        return treatment;
    }

    @Override
    public void setTreatment(Treatment treatment) {
        this.treatment = treatment;
    }

    @Override
    public List<PlanProcedure> getPlanProcedureList() {
        return planProcedureList;
    }

    @Override
    public void setPlanProcedureList(List<PlanProcedure> planProcedureList) {
        this.planProcedureList = planProcedureList;
    }

    @Override
    public ArrayList<PlanProcedure> getRes() {
        return res;
    }

    @Override
    public void setRes(ArrayList<PlanProcedure> res) {
        this.res = res;
    }

    @Override
    public PlanProcedure getPlanProcedure() {
        return planProcedure;
    }

    @Override
    public void setPlanProcedure(PlanProcedure planProcedure) {
        this.planProcedure = planProcedure;
    }
}
